#include "client.h"

int RecvFlagAndMsg(int fd, char* command) {
    int ret, flag;
    Train_t train;
    ret = recv(fd, &flag, sizeof(int), MSG_WAITALL);
    if(ret == -1) {
        perror("recv");
        return -1;
    } else if(ret == 0) {
        return -1;
    }
    if(flag == -1) {
        //服务器端处理命令出现错误，此时需要重新输入命令
        printf("error in %s\n", command);
        return 0;
    } else {
        int length;
        memset(&train, 0, sizeof(train));
        ret = recv(fd, &length, sizeof(int), MSG_WAITALL);
        if(ret < 0) {
            perror("recv");
            return -1;
        } else if(ret == 0) {
            return -1;
        }
        if(length == 0) {
            printf("%s\n", train.buf);
            return 0;
        }
        ret = recv(fd, train.buf, length, MSG_WAITALL);
        if(ret < 0) {
            perror("recv");
            return -1;
        } else if(ret == 0 && ret != train.lenth) {
            return -1;
        }
        printf("%s\n", train.buf);
        return 0;
    }
}

int RecvFlagOnly(int fd, char* command) {
    int ret, flag;
    ret = recv(fd, &flag, sizeof(int), MSG_WAITALL);
    if(ret == -1) {
        perror("recv");
        return -1;
    } else if(ret == 0) {
        return -1;
    }
    if(flag == -1) {
        //服务器端处理命令出现错误，此时需要重新输入命令
        printf("error in %s\n", command);
    }
    return 0;
}

